const skillList = [
    {
        name: "Bounce",
        number: 1
    },
    {
        name: "Pierce",
        number: 2
    },
    {
        name: "Gun",
        number: 1
    },
    {
        name: "Guard",
        number: 2
    }
]

const attributeList = [
    {
        name: "HP",
        number: 8
    },
    {
        name: "Damage",
        number: 10
    },
    {
        name: "Speed",
        number: 15
    },
    {
        name: "Attack Speed",
        number: 15
    },
    {
        name: "Life Steal",
        number: 8
    },
    {
        name: "Crit Chance",
        number: 8
    },
    {
        name: "Dodge",
        number: 8
    },
    {
        name: "Knockback",
        number: 8
    },
    {
        name: "Attack Range",
        number: 25
    },
    {
        name: "Pickup Range",
        number: 25
    }
]
const buffTypeList = ["speed", "attack_speed", "pickup_range"]
// 图片管理器
const ImageManager = (function () {
    const cache = {};
    const loadingPromises = {};
    // 预加载图片
    function preload(url) {
        if (!loadingPromises[url]) {
            loadingPromises[url] = new Promise((resolve, reject) => {
                const img = new Image();
                img.onload = () => {
                    cache[url] = img;
                    resolve(img);
                };
                img.onerror = reject;
                img.src = url;
            });
        }
        return loadingPromises[url];
    }

    // 获取已加载图片
    function get(name) {
        return cache[`img/${name}.svg`] || null;
    }

    return {
        preload,
        get
    };
})();

// 提前预加载所有图片（保持你原来的直观写法）

//暂停后刷新有误，待修改
ImageManager.preload('img/plant.svg');
ImageManager.preload('img/speed.svg');
ImageManager.preload('img/attack_speed.svg');
ImageManager.preload('img/pickup_range.svg');

var plantImage = new Image();
plantImage.src = 'img/plant.svg';
var speedImage = new Image();
speedImage.src = 'img/speed.svg';
var attackSpeedImage = new Image();
attackSpeedImage.src = 'img/attack_speed.svg';
var pickupRangeImage = new Image();
pickupRangeImage.src = 'img/pickup_range.svg';
var missileImage = new Image();
missileImage.src = 'img/missile.svg';
var sprintImage = new Image();
sprintImage.src = 'img/sprint.svg';

var updateFPSTime = 0;
var lastTime = 0;
var drawInterval = 15

// 1. 获取设备像素比（DPR）
var dpr = window.devicePixelRatio || 1;

var screenW = window.innerWidth
var screenH = window.innerHeight
var canvasW = screenW > 750 ? 750 : screenW
var canvasH = screenH

$("#blood-box").css({
    left: screenW / 2 - canvasW / 2
})
$("#buff-box").css({
    right: screenW / 2 - canvasW / 2 + 60
})
$("#fps").css({
    right: screenW / 2 - canvasW / 2
})
$(".pause-box").css({
    right: screenW / 2 - canvasW / 2
})

$(".action-box").hide()
$(".action").hide()
var begin = undefined
var baseW = $("body").width()
var baseR = 10 * dpr

var canvas = $("#myCanvas")[0];
canvas.style.width = `${canvasW}px`;
canvas.style.height = `${canvasH}px`;

canvas.width = canvasW * dpr;
canvas.height = canvasH * dpr;

var ctx = canvas.getContext('2d');
ctx.scale(dpr, dpr);


var foodList = []
var enemyList = []
var score = 0
var grade = 0
var damage = 1;//伤害
var speed = 1.2;//移速
var attackSpeed = 2;//攻速
var lifeSteal = 0//吸血
var critChance = 0.1//暴击
var dodge = 0//闪避
var knockBack = 4;//击退
var attackRange = 1//攻击范围
var pickupRange = 1//拾取范围
var bounce = 0//反弹
var pierce = 0//贯穿

//buff
var speedBuff = 1
var attackSpeedBuff = 1
var pickupRangeBuff = 1

var speedBuffDuration = 0
var attackSpeedBuffDuration = 0
var pickupRangeBuffDuration = 0

var gunNumber = 1
var guardNumber = 0
var enemySpeed = 1;
var angle = 0; // 设定角度
var r = baseR
var battlegroundX = -canvasW / 2 * dpr;
var battlegroundY = -canvasH / 4 * dpr;
var battlegroundW = canvasW * 2 * dpr;
var battlegroundH = canvasH * 1.5 * dpr;
var myX = canvasW / 2 * dpr;
var myY = canvasH / 2 * dpr;

var canvasW_dpr = canvasW * dpr;
var canvasH_dpr = canvasH * dpr;

var gunList = []
var bulletList = []
var guardList = []
var rokList = []
var sparkList = []
var driftList = []
var boomSparkList = []
var explosionList = []
var deductionList = []
var corpseList = []
var enemyBulletList = []
var plantList = []
var plantInterval = 60
var buffList = []
var shape = 0
var baseHP = 20
var currentHP = 20
var isUpdate = false

var invincible = 0
var baseInvincible = 10
var myDirection = "right"
//0：未开始、1：进行中、2：暂停、3：已结束、4：升级
var gameState = 1

var absorbInterval = 0
var driftInterval = 0

var resourceScore = 15

var chooseSkill = {}

var commonNumber = 0
var bubbleNumber = 0
var machineNumber = 0
var chargeNumber = 0
var tankNumber = 0
var boomNumber = 0
var trapNumber = 0
var missileNumber = 0

const BASE_BUFF_DURATION = 1000

const UPGRADE_FREQUENCY = 3


var twinkleCount = 0; // 初始化调用次数为 0
const frequency = 0.06; // 定义频率，可调整周期

const getTrigonometricValue = () => {
    const angle = twinkleCount * frequency; // 计算角度
    const value = Math.sin(angle); // 使用正弦函数计算值
    twinkleCount++; // 调用次数加 1

    // 当角度达到 2π 时，重置 twinkleCount
    if (angle >= 2 * Math.PI) {
        twinkleCount = 0;
    }
    return value;
};

var trigonometricValue = 0
